package com.falcon.mvc.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/** 日志处理: 打印请求参数和响应结果
 * @author zongf
 * @date 2024-02-04
 **/
@Slf4j
@Component
@Aspect
public class LogTimeAspect {

    // 切面: 切 @LogTime 注解
    @Pointcut("@annotation(com.falcon.mvc.anno.LogTime)" )
    public void pointCut() {}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getMethod().getName();

        // 执行方法
        Object response = null;
        try {
            response = joinPoint.proceed();
        }finally {
            log.info("方法耗时, 方法名称:{}.{}, 耗时:{}", className, methodName, (System.currentTimeMillis() - start));
        }

        return response;
    }

}
